package com.auntvt.timo.jpax.core.quick;

import com.auntvt.timo.jpax.core.interfaces.JpaxFunction;
import com.auntvt.timo.jpax.core.utils.LambdaUtil;
import org.springframework.dao.InvalidDataAccessApiUsageException;

/**
 * QuickSpecification Lambda操作
 *
 * @author 小懒虫 <auntvt@163.com>
 * @date 2021/7/31
 */
public class LambdaQuick<T> extends AbstractJpaQuick<T, LambdaQuick<T>, JpaxFunction<T, ?>> {

    private static final long serialVersionUID = -1475011242344897549L;

    /**
     * 子类创建一个本身的实例对象
     */
    @Override
    protected LambdaQuick<T> instance() {
        return new LambdaQuick<>();
    }

    /**
     * 将字段名称对象格式化为字符串类型，由子类负责实现
     *
     * @param fieldName 字段名称对象
     * @return 字段名
     */
    @Override
    protected String fieldNameToString(JpaxFunction<T, ?> fieldName) {
        String name = LambdaUtil.getFieldName(fieldName);
        if (name == null || name.trim().length() == 0) {
            throw new InvalidDataAccessApiUsageException("字段系列化失败，无法获得字段名称！");
        }
        return name;
    }

    /**
     * 转换为Quick
     *
     * @return Quick<T>
     */
    public Quick<T> quick() {
        return this.convert(new Quick<T>());
    }
}
